package cn.edu.sasu.service.Impl;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.common.constants.Constant;
import cn.edu.sasu.domain.dto.SysDoctorDTO;
import cn.edu.sasu.domain.entity.SysDoctor;
import cn.edu.sasu.domain.entity.SysHospital;
import cn.edu.sasu.mapper.SysDoctorMapper;
import cn.edu.sasu.service.SysDoctorService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 2024/4/27
 *
 * @author ganxue
 */
@Service
public class SysDoctorServiceImp implements SysDoctorService {
    @Resource
    private SysDoctorMapper sysDoctorMapper;


    @Override
    public LayuiRespResult doctorList(Integer page, Integer limit) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysDoctor> sysDepts = sysDoctorMapper.doctorList(Constant.DELETE_FLAG_YES,offset,rows);
        Integer totalSize = sysDoctorMapper.countDoctors(Constant.DELETE_FLAG_YES);
        return LayuiRespResult.success(totalSize,sysDepts);
    }

    @Override
    public List<SysDoctor> getDoctors(Integer deleteFlag) {
        return sysDoctorMapper.getAllDoctor(deleteFlag);
    }

    @Override
    public RespResult delSysHospitalByDoctorId(Integer doctorId) {
        sysDoctorMapper.updateSysDoctorDeleteFlagByDoctorId(doctorId,Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public RespResult batchdel(Integer[] ids) {
        sysDoctorMapper.updateSysDoctorDeleteFlagByDoctorIds(Arrays.asList(ids),Constant.DELETE_FLAG_YES);
        return RespResult.success("删除成功！");
    }

    @Override
    public LayuiRespResult search(Integer page, Integer limit, String doctorName, String professional, Integer hospitalId, Byte status) {
        Integer offset = (page - 1) * limit;
        Integer rows = limit;
        List<SysHospital> sysHospitals = sysDoctorMapper.searchSysDoctorList(Constant.DELETE_FLAG_YES,offset,rows,doctorName,professional,hospitalId,status);
        Integer totalSize = sysDoctorMapper.countSearchSysDoctor(Constant.DELETE_FLAG_YES,doctorName,professional,hospitalId,status);
        return LayuiRespResult.success(totalSize,sysHospitals);
    }

    @Override
    public RespResult save(SysDoctorDTO sysDoctorDTO) {
        SysDoctor doctor = sysDoctorMapper.findSysDoctorMapperByDoctorName(sysDoctorDTO.getDoctorName());
        if(doctor != null){
            return RespResult.error("该用户名已经存在，请重新输入！");
        }
        // 对象拷贝
        SysDoctor sysDoctor = BeanUtil.copyProperties(sysDoctorDTO,SysDoctor.class);
        sysDoctorMapper.save(sysDoctor);
        return RespResult.success("添加成功");
    }

    @Override
    public RespResult update(SysDoctorDTO sysDoctorDTO) {
        SysDoctor sysDoctor = BeanUtil.copyProperties(sysDoctorDTO,SysDoctor.class);
        sysDoctorMapper.update(sysDoctor);
        return  RespResult.success("修改成功");
    }
}
